import { router_item } from "@/routes";
import { matchRoute } from "@/util/utils";
import { Navigate, useLocation } from "react-router-dom";

function AutoRouter(props : {children:JSX.Element}){

    const {pathname} = useLocation();
    
    const route = matchRoute(pathname,router_item);
    if(route && route.meta && route.meta.noAuth){
        return props.children;
    }

    // if(pathname.startsWith("/login")){
    //     return props.children;
    // }
    const token = localStorage.getItem("token");
    if(token){
        return props.children;
    }else{
        return <Navigate to="/login"/>
    }
}

export default AutoRouter;